#ifndef _CONVERTER_H
#define _CONVERTER_H

#include <string>
#include <stdlib.h>
#include "../../LibUtil/LibUtil.h"
USING_LIB_UTIL
#include "XmlBind.h"
using namespace std;

XML_BIND_NAMESPACE_BEGIN

template<class T> string ConvertToString(const T& inValue)
{
	int& intValue = (int&)inValue;
	return XML_BIND_NAMESPACE::ConvertToString(intValue);
}

XML_TOOL_EXPORT string ConvertToString(const int& intValue) ;
XML_TOOL_EXPORT string ConvertToString(const unsigned int& intValue);
XML_TOOL_EXPORT string ConvertToString(const short& shortValue);
XML_TOOL_EXPORT string ConvertToString(const unsigned short& shortValue);
XML_TOOL_EXPORT string ConvertToString(const long& longValue);
XML_TOOL_EXPORT string ConvertToString(const unsigned long& longValue);
XML_TOOL_EXPORT string ConvertToString(const long long& longlongValue);
XML_TOOL_EXPORT string ConvertToString(const unsigned long long& longlongValue);
XML_TOOL_EXPORT string ConvertToString(const double& doubleValue);
XML_TOOL_EXPORT string ConvertToString(const float& floatValue);
XML_TOOL_EXPORT string ConvertToString(const bool& boolValue);
XML_TOOL_EXPORT string ConvertToString(const char* szValue);
XML_TOOL_EXPORT string ConvertToString(const wchar_t* szValue);
XML_TOOL_EXPORT string ConvertToString(const string& szValue);
XML_TOOL_EXPORT string ConvertToString(const wstring& szValue);

template<class CharType> string ConvertToString(const CharType* szValue, const string& inEncoding, const string& outEncoding)
{
	char* szOut = NULL;
	EncodingHelper::TransformCharset(szValue, &szOut, inEncoding.c_str(), outEncoding.c_str());
	string text(szOut);
	EncodingHelper::FreeString(szOut);
	return text;
}

template<class StringType> string ConvertToString(const StringType& szValue, const string& inEncoding, const string& outEncoding)
{
	return ConvertToString(szValue.c_str(), inEncoding.c_str(), outEncoding.c_str());
}

template<class T> void ConvertFromString(const string& text, T* inValue)
{
	int intValue = -1;
	XML_BIND_NAMESPACE::ConvertFromString(text, &intValue);
	*inValue = (T)intValue;
}

XML_TOOL_EXPORT void ConvertFromString(const string& text, int* intValue);
XML_TOOL_EXPORT void ConvertFromString(const string& text, unsigned int* intValue);
XML_TOOL_EXPORT void ConvertFromString(const string& text, short* shortValue);
XML_TOOL_EXPORT void ConvertFromString(const string& text, unsigned short* shortValue);
XML_TOOL_EXPORT void ConvertFromString(const string& text, long* longValue);
XML_TOOL_EXPORT void ConvertFromString(const string& text, unsigned long* longValue);
XML_TOOL_EXPORT void ConvertFromString(const string& text, long long* longValue);
XML_TOOL_EXPORT void ConvertFromString(const string& text, unsigned long long* longValue);
XML_TOOL_EXPORT void ConvertFromString(const string& text, double* doubleValue);
XML_TOOL_EXPORT void ConvertFromString(const string& text, float* floatValue);
XML_TOOL_EXPORT void ConvertFromString(const string& text, bool* boolValue);
XML_TOOL_EXPORT void ConvertFromString(const string& text, char** szValue);
XML_TOOL_EXPORT void ConvertFromString(const string& text, wchar_t** szValue);
XML_TOOL_EXPORT void ConvertFromString(const string& text, string* szValue);
XML_TOOL_EXPORT void ConvertFromString(const string& text, wstring* szValue);

template<class CharType> void ConvertFromString(const string& text, CharType** szValue, 
											  const string& inEncoding, const string& outEncoding)
{
	EncodingHelper::TransformCharset(text.c_str(), szValue, inEncoding.c_str(), outEncoding.c_str());
}

template<class CharType> void ConvertFromString(const string& text, 
											  basic_string<CharType, char_traits<CharType>, allocator<CharType> >* szValue, 
											  const string& inEncoding, const string& outEncoding)
{
	CharType* szText = NULL;
	EncodingHelper::TransformCharset(text.c_str(), &szText, inEncoding.c_str(), outEncoding.c_str());
	*szValue = szText;
	EncodingHelper::FreeString(szText);
}

XML_BIND_NAMESPACE_END

#endif //_CONVERTER_H